<?php

namespace app\admin\controller;

use think\Request;
use app\admin\model\Teacher as TeacherModel;
use app\admin\model\Student as StudentModel;
use app\admin\model\User as UserModel;
use app\admin\model\Grade as GradeModel;
use think\Db;
use think\Paginator;

class Recycle extends Base
{
    public function student_list()
    {
        //定义空数组
        $studentList = array();
        //seo渲染
        $this->view->assign('title', '学生列表');
        $this->view->assign('keywords', '');
        $this->view->assign('desc', '中小学教育管理平台');
        //获取所有学生数据
        $sql = "select a.id,a.name,a.sex,a.age,a.mobile,a.email,a.start_time,a.status,b.name as grade from student as a LEFT JOIN grade as b on b.id=a.grade_id where a.delete_time IS NOT NULL GROUP BY a.id";
        $student = Db::query($sql);
        //给数组中的每个模板对象添加班级关联数据,
        foreach ($student as $value) {
            $data = [
                'id' => $value['id'],
                'name' => $value['name'],
                'sex' => $value['sex'],
                'age' => $value['age'],
                'mobile' => $value['mobile'],
                'email' => $value['email'],
                'start_time' => $value['start_time'],
                'status' => $value['status'],
                'grade' => isset($value['grade']) ? $value['grade'] : '<span style="color:red;">未分配</span>',
            ];
            $studentList[] = $data;
        }
//        print_r(unique_arr($studentList,true));
//        var_dump($studentList);die;
        $this->view->assign('studentList', $studentList);

        return $this->view->fetch('student_list');

    }

    //删除学生信息
    public function student_del(Request $request)
    {
        $student_id = $request->param('id');
        StudentModel::destroy($student_id, true);

    }

    //恢复学生信息
    public function student_recover(Request $request)
    {
        $student_id = $request->param('id');
//        var_dump($student_id);die;
        StudentModel::update(['delete_time' => NULL], ['is_delete' => 1, 'id' => $student_id]);
    }

    public function teacher_list()
    {
        //定义空数组
        $teacherList = array();
        //获取所有教师表teacher数据
        $sql = "select a.id,a.name,a.degree,a.school,a.mobile,a.hiredate,a.status,b.name as grade from teacher as a LEFT JOIN grade as b on b.id=a.grade_id where a.delete_time IS NOT NULL";
        $teacher = Db::query($sql);
//        var_dump($teacher);die;

        //遍历teacher表
        foreach ($teacher as $value) {
//            var_dump($$value);die;
            $data = [
                'id' => $value['id'],  //主键
                'name' => $value['name'],  //姓名
                'degree' => $value['degree'],  //学历
                'school' => $value['school'],  //毕业学校
                'mobile' => $value['mobile'],  //手机号
                'hiredate' => $value['hiredate'],  //入职时间
                'status' => $value['status'],  //当前启用状态
                //用关联方法grade属性方式访问grade表中数据
                'grade' => isset($value['grade']) ? $value['grade'] : '<span style="color:red;">未分配</span>',
            ];
            //每次关联查询结果,保存到数组   $teacher中
            $teacherList[] = $data;
        }

        $this->view->assign('teacherList', $teacherList);


        return $this->view->fetch('teacher_list');
    }

    //删除教师信息
    public function teacher_del(Request $request)
    {
        $teacher_id = $request->param('id');
        TeacherModel::destroy($teacher_id, true);

    }

    //恢复教师信息
    public function teacher_recover(Request $request)
    {
        $teacher_id = $request->param('id');
        TeacherModel::update(['delete_time' => NULL], ['is_delete' => 1, 'id' => $teacher_id]);
    }

    public function grade_list()
    {
        //定义空数组
        $gradeList = array();
        //获取所有班级表数据
        $sql = "select a.id,a.name,a.length,a.price,a.status,a.create_time,b.name as teacher from grade as a LEFT JOIN teacher as b on a.id=b.grade_id where a.delete_time IS NOT NULL";
        $grade = Db::query($sql);
//        echo Db::getLastSql();

        //遍历grade表
        foreach ($grade as $value) {
//                var_dump($value);
            $data = [
                'id' => $value['id'],
                'name' => $value['name'],
                'length' => $value['length'],
                'price' => $value['price'],
                'status' => $value['status'],
                'create_time' => $value['create_time'],
                //用关联方法teacher属性方式访问teacher表中数据
                'teacher' => isset($value['teacher']) ? $value['teacher'] : '<span style="color:red;">未分配</span>',
            ];
            //每次关联查询结果,保存到数组$gradeList中
            $gradeList[] = $data;
        }

        $this->view->assign('gradeList', $gradeList);


        return $this->view->fetch('grade_list');

    }

    //删除班级信息
    public function grade_del(Request $request)
    {
        $grade_id = $request->param('id');
        GradeModel::destroy($grade_id, true);

    }

    //恢复班级信息
    public function grade_recover(Request $request)
    {
        $grade_id = $request->param('id');
        GradeModel::update(['delete_time' => NULL], ['is_delete' => 1, 'id' => $grade_id]);
    }

    public function user_list()
    {

        $sql = "select * from admin where delete_time is not null";
        $list = Db::query($sql);

        $this->view->assign('list', $list);

        //渲染管理员列表模板
        return $this->view->fetch('user_list');

    }

    //删除管理员信息
    public function user_del(Request $request)
    {
        $user_id = $request->param('id');
        UserModel::destroy($user_id, true);

    }

    //恢复管理员信息
    public function user_recover(Request $request)
    {
        $user_id = $request->param('id');
        UserModel::update(['delete_time' => NULL], ['is_delete' => 1, 'id' => $user_id]);
    }

}